<template>
  <el-container>
    <el-header>
        <span style="font-weight:bold; color:blue;">添加订单</span>
    </el-header>
    <el-form :model="data" ref="data" label-width="100px" class="demo-dynamic">
      <el-form-item
        prop="code"
        label="编码"
        :rules="[
      { required: true, message: '请输入编码', trigger: 'blur' },
      { type: 'string', message: '请输入正确的编码', trigger: ['blur', 'change'] }
    ]"
      >
        <el-input v-model="data.code" maxlength="12" clearable="true" style="width:50%"></el-input>
      </el-form-item>
      <el-form-item
        prop="name"
        label="名字"
        :rules="[
      { required: true, message: '请输入名字', trigger: 'blur' },
      { type: 'string', message: '请输入正确的名字', trigger: ['blur', 'change'] }
    ]"
      >
        <el-input v-model="data.name" maxlength="6" clearable="true" style="width:50%"></el-input>
      </el-form-item>
      <el-form-item
        prop="price"
        label="价格"
        :rules="[
      { required: true, message: '请输入价格', trigger: 'blur' },
      { type: 'number', message: '请输入正确的价格', trigger: ['blur', 'change'] }
    ]"
      >
        <el-input v-model.number="data.price" maxlength="11" clearable="true" style="width:50%"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('data')">提交</el-button>
        <el-button @click="resetForm('data')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      data: {
        code: "",
        name: "",
        price: ""
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>